﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QLBV.Models;
using System.Web.Security;
using QLBV.Controllers;

namespace QLBV.Controllers
{
    public class LoginController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Login(LoginModel model)
        {
            if (ModelState.IsValid)
            {
                var db = new QLBVContext();
                string user = model.Username;
                string pass = model.Password;

                var nd = db.Employees.Where(nv => nv.Username == user && nv.Password == pass).FirstOrDefault();
                if (nd != null)
                {
                    @FormsAuthentication.SetAuthCookie(model.Username, true);
                    HttpContext.Session["username"] = model.Username;
                    Session["ID"] = nd.ID;
                    Session["username"] = nd.Username;
                    Session["HoTen"] = nd.Name;
                    return RedirectToAction("DSTin", "News");
                }
                else
                {
                    ModelState.AddModelError("", "Đăng nhập thất bại, vui lòng kiểm tra tài khoản!");
                }
            }
            return View();
        }
        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index", "Home");
        }
    }
}
